import canvasAbstract from "./canvasAbstract"
import model from "../model/bullet"
import tankModel from "./enemy"

class bullet extends canvasAbstract implements ICanvas {
  intervalId = null
  constructor() {
    super('bullet')
  }

  model(): BulletModelConstructor {
    return model
  }

  num(): number {
    return 0
  }

  stop(): void {
    // @ts-ignore
    clearInterval(this.intervalId)
  }

  render(): void {
    // 将画布置于最高处
    this.el.style.zIndex = '999'
    // 创建子弹
    // @ts-ignore
    this.intervalId = setInterval(() => {
      // 随机决定是否创建子弹
      if (Math.floor((Math.random() * 1000)) < 10) {
        this.createBullet()
      }
      this.renderModels()
    }, 10)
  }

  // 创建子弹
  protected createBullet() {
    tankModel.models.forEach(tank => {
      const isExists = this.models.some(m => {
        return m.tank === tank
      })
      if (!isExists) {
        this.models.push(new model(tank))
      }
    })
  }

  // 玩家子弹
  public addPlayerBullet(tank: IModel) {
    this.models.push(new model(tank))
  }
}

export default new bullet()
